package com.yb.web.config;

import java.math.BigInteger;

import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

@Configuration
public class JacksonConfig {
	
	@Bean
	public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
		Jackson2ObjectMapperBuilderCustomizer cunstomizer = new Jackson2ObjectMapperBuilderCustomizer() {
			@Override
			public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
				
				/**
				 * Long类型序列化
				 * js的 Number类型长度是17位；mysql使用bigint类型长度是20位。
				 * 比如使用idworker生成的id的长度是18位，传入前端会出现缺失精度问题。
				 * 全局转化Long类型为String类型，解决jackson序列化时传入前端缺失精度问题。
				 */
				jacksonObjectMapperBuilder.serializerByType(Long.TYPE, ToStringSerializer.instance);
				jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance);
				jacksonObjectMapperBuilder.serializerByType(BigInteger.class, ToStringSerializer.instance);
				
				/**
				 * Date类型序列化
				 */
				jacksonObjectMapperBuilder.timeZone("GMT+8");
				jacksonObjectMapperBuilder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
			}
		};
		
		return cunstomizer;
	}
}
